package com.jkc.xh.fragments;

import android.view.View;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.fragments.core.BaseFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.AssetInfoResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.widget.NavBar;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_my_money)
public class MyMoneyFragment extends BaseFragment {
    @ViewById(R.id.activityTitleName)
    NavBar activityTitleName;

    @ViewById(R.id.dta_money_text)
    TextView dta_money_text;
    @ViewById(R.id.enable_money_text)
    TextView enable_money_text;
    @ViewById(R.id.freeze_money_text)
    TextView freeze_money_text;
    @ViewById(R.id.stock_enable_money_text)
    TextView stock_enable_money_text;
    @ViewById(R.id.market_value_text)
    TextView market_value_text;
    @ViewById(R.id.profit_money_text)
    TextView profit_money_text;

    @AfterViews
    void initView() {
        activityTitleName.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                loadData();
            }
        });

        loadData();
    }

    private void loadData(){
        showProgress("正在查询");
        addJob(TraderManager.queryAssetInfo(new RestCallBack<AssetInfoResponse>() {


            @Override
            public void success(AssetInfoResponse assetInfoResponse) {
                dta_money_text.setText(assetInfoResponse.dta_money + "");
                freeze_money_text.setText(assetInfoResponse.freeze_money + "");
                profit_money_text.setText(assetInfoResponse.profit_money + "");
                market_value_text.setText(assetInfoResponse.market_value + "");
                stock_enable_money_text.setText(assetInfoResponse.stock_enable_money + "");
                enable_money_text.setText(assetInfoResponse.enable_money + "");
                if (assetInfoResponse.isProfit()) {
                    profit_money_text.setTextColor(getResources().getColor(R.color.text_color_red));
                } else {
                    profit_money_text.setTextColor(getResources().getColor(R.color.text_color_green));
                }
                hideProgress();
            }

            @Override
            public void failure(RestError error) {
                hideProgress();
            }
        }));
    }

    @Click(R.id.to_money_bill)
    void toMoneyBill() {
        MoneyBillFragment_.builder().build().startFragment(getContext());
    }
}
